package com.edu.framework.websocket.handler;

import cn.hutool.core.util.StrUtil;
import com.edu.framework.websocket.WebSocketHolder;
import com.edu.framework.websocket.message.base.Response;
import com.edu.framework.websocket.message.card.CardRequest;
import com.edu.framework.websocket.message.card.CardVO;
import com.edu.framework.websocket.message.studyscore.StudyScoreRequest;
import com.edu.framework.websocket.message.studyscore.StudyScoreVO;
import com.edu.framework.websocket.service.CardService;
import com.edu.framework.websocket.service.StudyScoreService;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

@Component
public class CardRequestHandler implements IHandler<CardRequest> {

    @Resource
    private CardService wsCardService;

    @Override
    public void execute(WebSocketSession session, CardRequest message) {
        if (message == null) {
            return;
        }
        Map<String, String> data = message.getData();
        String cardNo = data.get(CardRequest.CARD_NO);
        Response response = null;

        if (StrUtil.isBlank(cardNo)){
            response = Response.fail(CardRequest.TYPE, "卡号不能为空");
        } else {
            response = wsCardService.getCardInfo(cardNo);
        }

        try {
            WebSocketHolder.send(session,response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String getType() {
        return CardRequest.TYPE;
    }




}
